﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvModel;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ContactPersonDB
    {
        private static CvModelContainer _context = new CvModelContainer();

        //CREATE
        public static int CreateContactPerson(ContactPerson contactPerson)
        {
            try
            {
                var query = _context.ContactPerson.Add(contactPerson);
                return _context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //READ
        public static ContactPerson GetContactPerson(int workAdId)
        {
            try
            {
                var contactPerson =
    (from w in _context.WorkAd
     from a in _context.Admin
     from c in _context.ContactPerson
     where w.WorkAdId == workAdId && w.AdminId == a.AdminId && c.WorkAdminId == a.AdminId
     select c).First();
                return contactPerson;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static List<ContactPerson> GetAllContactPersonFromWorkAdminId(int workAdminId)
        {
            try
            {
                var contactPerson =
     (from c in _context.ContactPerson
     where c.WorkAdminId == workAdminId
     select c).ToList();
                return contactPerson;
            }
            catch (Exception)
            {

                throw;
            }
        }
        //UPDATE
        public static int UpdateContactPerson(ContactPerson contactPerson)
        {
            try
            {
                _context.ContactPerson.AddOrUpdate(contactPerson);
                return _context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        //DELETE
        public static int DeleteContactPerson(int contactPersonId)
        {
            try
            {
                var query =
                    (from c in _context.ContactPerson
                     where c.ContactPersonId == contactPersonId
                     select c).Single();

                _context.ContactPerson.Remove(query);

                return _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
